require(['./config'], () => {
    require(['template', 'header', 'footer'], (template,header) => {
        class Shopping {
            constructor() {
                this.init()
               
            }
            init() {
                let cart = localStorage.getItem('cart')
                console.log(111);
                if (cart) {
                    this.cart = JSON.parse(cart)
                    $('#emptybox').hide()    
                    this.render()
                    this.calc()
                    this.totmoney()
                } else {
                    $('#emptybox').show()
                }
            }
            render() {
                $('#largebox').html(template('srcshop', { cartlist: this.cart }))
            }
          calc() {
                const _this = this
                return new Promise(resolve => {
                    $('.btnjia').each(function (index) {                 
                    const id = Number($(this).parents('li').attr('data-id'))
                            console.log(id);        
                        $(this).parents('li').on('click', '.btnjia', function (e) {
                           
                            
                            let cart = localStorage.getItem('cart')
                            if (cart) {
                                cart = JSON.parse(cart)
                                console.log(_this.cart,cart);
                                _this.cart = cart.map(shop=>{
                                    if (shop.id === id) {
                                      shop.count++
                                      $(this).parents('li').find('.xiaoji').html('小计：￥'+(shop.count)*(shop.price).slice(1))          
                                    }
                                    return shop
                                })
                                localStorage.setItem('cart', JSON.stringify(cart))
                                console.log(_this,this,$(this));
                                header.calcnum()
                               _this.totmoney()
                                $(this).parents('.btninfo').find('.shopnum').html(cart[index].count);
                            }
                        })      
                    })
                    $('.btnjian').each(function (index) {
                        const id = Number($(this).parents('li').attr('data-id'))
                                console.log(id);          
                            $(this).parents('li').on('click', '.btnjian', function (e) {
                                let cart = localStorage.getItem('cart')
                                if (cart) {
                                    cart = JSON.parse(cart)
                                    _this.cart = cart.map(shop=>{
                                        if (shop.id === id) {
                                          shop.count--
                                          if(shop.count<1){
                                          shop.count=1}
                                          $(this).parents('li').find('.xiaoji').html('￥'+(shop.count)*(shop.price).slice(1))
                                        }
                                        return shop
                                    })
                                    console.log(_this.cart);
                                    localStorage.setItem('cart', JSON.stringify(cart))
                                    $(this).parents('.btninfo').find('.shopnum').html(cart[index].count);
                                    header.calcnum()
                                    _this.totmoney()
                                }
                            })            
                       $(this).parents('li').on('click','.delete',function(){
                              let cart = localStorage.getItem('cart')
                              cart=JSON.parse(cart)
                              console.log(cart);
                              console.log(cart.length);
                             if(cart.length > 1) {
                                 for(var i =0;i<cart.length;i++){
                                    if(cart[i].id===id){
                                        var newcart
                                        newcart=cart.splice(i,1)
                                  }
                                 }
                                 $(this).parents('li').remove()
                                 localStorage.setItem('cart', JSON.stringify(cart)) 
                                 const money = cart.reduce((sum, shop) => {
                                    sum += (shop.price).slice(1) * shop.count
                                    return sum
                                }, 0)
                                $('#moneytoo').html('￥' + money)
                                 console.log(cart);
                              }
                             else if(cart.length=1){
                                localStorage.removeItem('cart')
                                $(this).parents('li').remove()
                                $('#moneytoo').html('￥' + 0 )
                                console.log(id);
                             }
                            header.calcnum()
                          })
                        })
                })
            }
            totmoney() {
                const money = this.cart.reduce((sum, shop) => {
                    sum += (shop.price).slice(1) * shop.count
                    return sum
                }, 0)
                $('#moneytoo').html('￥' + money)
            }
        }
        new Shopping()
    })
})